<?php
namespace application\admin\panels;

class view_users extends panel {
	public $name = 'View Users';
	
	public static function js() {
		?>
		<script type="text/javascript">
			$(document).ready(function() {
				$('.admin-content-display').click(function() {
					var display = $(this).parent('td').parent('tr').next('tr');
					if($(display).is(':hidden')) {
						$(display).show();
						$(this).html('Admin Content &uarr;');
					}
					else {
						$(display).hide();
						$(this).html('Admin Content &darr;');
						$(display).children('td').children('table').children('tbody').children('tr').children('td').children('.remove-admin').attr('checked', false);
					}
				});
				$('#user-bulk-actions').change(function() {
					if($(this).val() == 'admin-of') {
						$('#admin-of-select').show();
					}
					else {
						$('#admin-of-select').hide();
					}
				});
				$('#admin-content-search').keyup(function() {
					var search = $(this).val();
					$.ajax({
						url: '<?=CN_URL?>ajax/content/',
						dataType:'json',
						data: {
							search:search
						},
						success: function(data) {
							$('#admin-of-content').html('');
							for(var i=0;i<data.length;i++) {
								$('#admin-of-content').append('<option value="' + data[i].content_id + '">' + data[i].content_title + '</option>');
							}
						}
						
					
					
					});
				
				});
				
				$('.remove-admin').click(function() {
					$(this).parent('td').parent('tr').parent('tbody').parent('table').parent('td').parent('tr').prev('tr').children('td').children('.selectee-all').attr('checked', 'checked');
				});
			});
		</script>
		<?php
	}
	
	public function handleData() {
		if($_REQUEST['perform_bulk_actions']) {
			$users = $_REQUEST['action'];
			if(count($users)) {
				$userIn = "'".implode("', '", $users)."'";
				switch($_REQUEST['bulk_actions']) {
					case 'delete':
						DB()->query("DELETE FROM `cn_users` WHERE `user_id` IN ($userIn)");
						$this->addMessage('success', 'Selected user(s) successfully deleted.');
						break;
					case 'global':
						DB()->query("UPDATE `cn_users` SET `user_level`='2' WHERE `user_id` IN ($userIn)");
						$this->addMessage('success', 'Selected user(s) successfully promoted.');
						break;
					case 'remove-global':
						DB()->query("UPDATE `cn_users` SET `user_level`='0' WHERE `user_id` IN ($userIn)");
						$this->addMessage('success', 'Selected user(s) successfully demoted.');
						break;
					case 'admin-of':
						$content = $_REQUEST['admin_of_content'];
						foreach($users as $user) {
							DB()->query("INSERT INTO `cn_user_content_relations` (`ucr_content`, `ucr_interaction`, `ucr_user`) VALUES('$content', '99999', '$user')");
						}
						$this->addMessage('success', 'Selected user(s) successfully promoted.');
						break;
					case 'remove-admins':
						if(count($_REQUEST['remove_admin'])) {
							$removeAdmins = "'".implode("', '", $_REQUEST['remove_admin'])."'";
							DB()->query("DELETE FROM `cn_user_content_relations` WHERE `ucr_id` IN ($removeAdmins) AND `ucr_interaction`='99999'");
							$this->addMessage('success', 'Administrative privileges removed successfully.');
						}
						break;
				}
					
			}
		}
	}
	
	public function display() {
		global $concerto;
		$concerto->UsersQuery->initMainQuery();
		
		$shownColumns = DB()->getVarArray("SELECT `cf_display_name` FROM `cn_content_fields` WHERE `cf_content_type`='0' AND `cf_show`='1'");
		$shownColumnsCount = count($shownColumns);
		
		$this->displayMessages();

		
		?>
		
		<div class="box">
			<h3>View Users</h3>
			<form method="get">
				<input type="hidden" name="display" value="<?=getQueryVar('display')?>"/>
				<ul id="filter-content">
				<li><label for="status">User Level</label>
					<select name="status">
						<option <?=(getQueryVar('level') == 'all') ? 'selected="selected"' : ''?> value="all">All (<?=DB()->getCount("SELECT `user_id` FROM `cn_users`");?>)</option>
						<option <?=(getQueryVar('level') == 'global') ? 'selected="selected"' : ''?> value="global">Global Administrator (<?=DB()->getCount("SELECT `user_id` FROM `cn_users` WHERE `user_level`='2'");?>)</option>
						<option <?=(getQueryVar('level') == 'local') ? 'selected="selected"' : ''?> value="pending">Local Administrator (<?=DB()->getCount("SELECT `user_id` FROM `cn_users` WHERE `user_level`='1'");?>)</option>
						<option <?=(getQueryVar('level') == 'normal') ? 'selected="selected"' : ''?> value="normal">Normal (<?=DB()->getCount("SELECT `user_id` FROM `cn_users` WHERE `user_level`='0' || `user_level`=NULL");?>)</option>
					</select>
				</li>
				<li><label>Order by</label>
				<select name="order">
					<option <?=(getQueryVar('order') == 'username') ? ' selected="selected"' : '';?> value="username">Username</option>
					<option <?=(getQueryVar('order') == 'email') ? ' selected="selected"' : '';?> value="email">E-Mail</option>
					<option <?=(getQueryVar('order') == 'id') ? ' selected="selected"' : '';?> value="id">ID</option>
				</select>
				</li>
				<li>
					<label for="search">Search</label>
					<input type="text" name="search" value="<?=getQueryVar('search')?>"/>
				</li>
				<li>
					<input type="submit" value="Filter" name="filter_content" class="small-button"/>
				</li>
				</ul>
			</form>
			<form method="post">
			<table class="admin-table" width="100%">
				<thead>
					<tr>
						<th class="first" width="2%"><input type="checkbox" class="select-all"/></th>
						
						<?php foreach($shownColumns as $column) : ?>
						<th><?=$column?></th>
						<?php endforeach;?>
						<th width="15%">Actions</th>
						<th class="last" width="2%"></th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<th class="first" width="2%"><input type="checkbox" class="select-all"/></th>
						<?php foreach($shownColumns as $column) : ?>
						<th><?=$column?></th>
						<?php endforeach;?>
						<th width="15%">Actions</th>
						<th class="last" width="2%"></th>
					</tr>
				</tfoot>
				
				<tbody>
					
					<?php $style = ''; ?>
					<?php $innerStyle = '';?>
					<?php while(hasUsers()) : initUser();?>
					
					<tr<?=$style?>>
						<td><input type="checkbox" name="action[]" value="<?php displayUserField('ID')?>" class="selectee-all"/></td>				
						<?php foreach($shownColumns as $value) : ?>
						<td><?php displayUserField($value);?></td>
						<?php endforeach;?>
						<td><a href="<?=CN_URL?>user/?id=<?=getUserField('ID')?>">View</a> | <a href="<?=CN_URL?>?view=form&form=edit_profile&id=<?=getUserfield('ID')?>">Edit</a>
							<?php $adminContent = DB()->getResults("SELECT * FROM `cn_content` INNER JOIN `cn_user_content_relations` ON `ucr_content`=`content_id` INNER JOIN `cn_content_types` ON `content_type`=`ct_id` WHERE `ucr_user`='".getUserField('ID')."' AND `ucr_interaction`='99999' ORDER BY `ct_id`, `content_title`");?>
							<?php if(count($adminContent)) : ?>
								| <a href="javascript:void();" class="admin-content-display">Admin Content &darr;</a>
							<?php endif;?>
						
						</td>
						<td></td>
					</tr>
					<tr style="display:none;" <?=$innerStyle?>>
						
						
						<?php if(count($adminContent)) : ?>
						<td width="100%" colspan="<?=($shownColumnsCount + 2)?>">
							<table  class="admin-table-inner" width="100%">
								<thead>
									<tr><th class="first" width="5%"></th><th width="50%">Title</th><th width="30%">Content Type</th><th width="20%">Remove</th><th class="last" width="5%"></th></tr>
								</thead>
								<tbody>
								<?php foreach($adminContent as $content) : ?>
								<tr>
									<td></td>
									<td><?=$content->content_title?></td>
									<td><?=$content->ct_title?></td>
									<td><input type="checkbox" name="remove_admin[]" class="remove-admin" value="<?=$content->ucr_id?>"/></td>
									<td></td>
								</tr>
								<?php endforeach;?>
								</tbody>
								<tfoot>
									<tr><th class="first" width="5%"></th><th width="50%">Title</th><th width="30%">Content Type</th><th width="20%">Remove</th><th class="last" width="5%"></th></tr>
								</tfoot>
							</table>
							
							
						</td>	
						<?php endif;?>
						
					</tr>
					<?php $style = ($style == ' class="alternate"') ? '' : ' class="alternate"';?>
					<?php $innerStyle = ($innerStyle == ' class="alternate admin-content-row"') ? ' class="admin-content-row"' : ' class="alternate admin-content-row"';?>
					<?php endwhile;?>
				</tbody>
			</table>
		</div>
		<div class="bulk-actions">
			
			<select id="user-bulk-actions" name="bulk_actions">
				<option value="">Bulk Actions</option>
				<option value="delete">Delete Selected</option>
				<option value="global">Make Global Administrators</option>
				<option value="remove-global">Remove as Global Administrators</option>
				<option value="admin-of">Make Administrator Of...</option>
				<option value="remove-admins">Remove Selected Administered Listings</option>
			</select>
			<div id="admin-of-select" style="display:none">
				Search Content: <input type="text" id="admin-content-search"/> <select id="admin-of-content" name="admin_of_content"></select>
			</div>
			<input type="submit" onclick="if($('#user-bulk-actions').val() == 'delete') return confirm('Are you sure you want to delete the selected user(s)?')" value="Go" id="perform-bulk-actions" name="perform_bulk_actions"/>
			</form>
		</div>
		<?php
	}
}
?>